package com.itqf.controller;

import com.itqf.bean.User;
import com.itqf.service.UserService;
import com.itqf.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * projectName:
 *
 * @author: 元
 * time: 2021/7/6 0006 20:02
 * description:用户controller层
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

//查询、删除、获取用GET,添加、更新用POST,前端传递JSON格式数据也用POST,不希望参数直接暴露在路径 使用post
    //1.查询显示所有用户以及全部信息
    @GetMapping("userList")
    public Object findAllUsers(){
        //1.参数接受 2.调用service
        ResultVO resultVO = userService.findAll();
        //3.响应数据
        return resultVO;
    }

    //2.根据id查询某个用户的详细信息
    @GetMapping("userInfo")
    public Object findUserById(int id){
        ResultVO resultVO = userService.findById(id);
        return resultVO;
    }

    //3.根据用户名查询,模糊查询
    @PostMapping(value = "search", consumes = "application/json;charset=utf-8")
    public Object search(@RequestBody String account){
        ResultVO resultVO = userService.findUser(account);
        return resultVO;
    }

    //4.添加用户
    @PostMapping(value = "add", consumes = "application/json;charset=utf-8")
    public Object save(@RequestBody User user) {
        ResultVO resultVO =userService.addUser(user);
        return resultVO;
    }

    //5.删除用户
    @GetMapping("remove")
    public Object removeUser(int id) {
        ResultVO resultVO = userService.removeUser(id);
        return resultVO;
    }
        //6.修改用户
     @PostMapping(value = "update", consumes = "application/json;charset=utf-8")
    public Object update (@RequestBody User user){
        ResultVO resultVO = userService.updateUser(user );

         return resultVO;
        }
    }





